package lec19graph.part0traversal;

public class GraphDFS {
    final static int INF = Integer.MAX_VALUE >> 1;
    static int n = 5;
    static int[] book = new int[n];
    static int[][] g = new int[][]{
            {0, 1, 1, INF, 1},
            {1, 0, INF, 1, INF},
            {1, INF, 0, INF, 1},
            {INF, 1, INF, 0, INF},
            {1, INF, 1, INF, 0}

    };
    static int sum = 0;//表示访问的节点

    public static void main(String[] args) {
        dfs(0);
    }

    private static void dfs(int cur) {
        System.out.println(cur);
        book[cur] = 1;//如果不用book 修改g也可以达到目的，但一般不这么做
        ++sum;
        if (sum == n) return;
        for (int i = 0; i < n; i++) {
            if (g[cur][i] == 1 && book[i] == 0) {
                book[i] = 1;
                dfs(i);
            }
        }


    }
}
